package com.cn.hnust.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.hnust.pojo.Message;

import com.cn.hnust.service.IMessageService;
import com.cn.hnust.util.Page;

@Controller
@RequestMapping("/message")
public class MessageController {

	@Autowired
	private IMessageService messageservice;

	@RequestMapping(value = "showMessage")
	public String message(Model model) {

		List<Message> message = messageservice.getUsers();
		model.addAttribute("message", message);
		return "message";
	}

	@RequestMapping(value = "/delmessage")
	@ResponseBody
	public Map<String, Object> delmessage(int id, Model model) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		messageservice.delmessage(id);
		int pageNow1 = 1;
		Page page = null;// new 对象在下面
		int totalCount = messageservice.getCount();// 获取当前用户总记录条数

//	}else if (pageNow != null && pageNow != "") {// 防止出现空指针异常
//			int pageNow1 = Integer.parseInt(pageNow);
			page = new Page(totalCount, pageNow1);// 这样写的好处，判断完成外面可以继续调用
		List<Message> message = messageservice.queryAllRecord(page);
		model.addAttribute("page", page);
		model.addAttribute("message", message);
		resultMap.put("result", "删除成功");
		return resultMap;
	}

	@RequestMapping(value = "messageadd")
	public String messageinsert(Message messages, Model model,String message_name) {
		int a = messageservice.insert(messages);
		int pageNow1 = 1;
		if (a > 0) {
			Page page = null;// new 对象在下面
			int totalCount = messageservice.getCount();// 获取当前用户总记录条数
	
//		}else if (pageNow != null && pageNow != "") {// 防止出现空指针异常
//				int pageNow1 = Integer.parseInt(pageNow);
				page = new Page(totalCount, pageNow1);// 这样写的好处，判断完成外面可以继续调用
			
			model.addAttribute("page", page);
		
			List<Message> message = messageservice.selectPersons(message_name, page);
			model.addAttribute("message", message);
			return "message";
		} else {
			return "messageAdd";
		}
	}

	/**
	 * 添加页面
	 */
	@RequestMapping(value = "messageAdd")
	public String MessageAdd() {
		return "messageAdd";
	}
	
	@RequestMapping(value="selectmh")
	public String selectmh(HttpServletRequest request,Model model){
		String message_names=request.getParameter("message_name");
		
		String message_name="%"+message_names+"%";
		
		int pageNow1 = 1;
		
		Page page = null;// new 对象在下面
		int totalCount = messageservice.getCount();// 获取当前用户总记录条数

//	}else if (pageNow != null && pageNow != "") {// 防止出现空指针异常
//			int pageNow1 = Integer.parseInt(pageNow);
			page = new Page(totalCount, pageNow1);// 这样写的好处，判断完成外面可以继续调用
		List<Message> message = messageservice.selectPersons(message_name, page);
		model.addAttribute("page", page);
		
		model.addAttribute("message", message);
		return "message";
	}
	
	@RequestMapping(value="/queryAll")
	 public String record(Model model, String pageNow){
      
       Page page = null;//new 对象在下面
       
      int totalCount = messageservice.getCount();//获取当前用户总记录条数
       
       if(pageNow != null && pageNow != "") {//防止出现空指针异常
           int pageNow1 = Integer.parseInt(pageNow);
           page = new Page(totalCount, pageNow1);//这样写的好处，判断完成外面可以继续调用
       }  
       List<Message> message = messageservice.queryAllRecord(page);//取得当前用户的所有操作记录
       model.addAttribute("page",page);
       model.addAttribute("message",message);
       
       return "message";
   }
	
	@RequestMapping(value = "/Messagesupdate")
	public String Messagesupdate(HttpServletRequest request, Model model,String message_name) {
		Message messages=messageservice.getUser(message_name);
		model.addAttribute("messages", messages);
		return "messageupdate";
	}
	
	@RequestMapping(value = "/messagesuccess")
	public String messagesuccess(int message_id,HttpServletRequest request, Model model) {
		String message_name=request.getParameter("message_name");
		String message_board=request.getParameter("message_board");
		int pageNow1=1;
		Message message = new Message();
		message.setMessage_id(message_id);
		message.setMessage_name(message_name);
		message.setMessage_board(message_board);
		messageservice.updateByPrimaryKey(message);
		Page page = null;// new 对象在下面
		int totalCount = messageservice.getCount();// 获取当前用户总记录条数

//	}else if (pageNow != null && pageNow != "") {// 防止出现空指针异常
//			int pageNow1 = Integer.parseInt(pageNow);
			page = new Page(totalCount, pageNow1);// 这样写的好处，判断完成外面可以继续调用
		
		model.addAttribute("page", page);
		List<Message> messages=messageservice.selectPersons(message_name, page);
		model.addAttribute("message", messages);
		
		
		return "message";
		
	}
}
